package com.google.code.head2head.gwtmodules.client.login;

import com.google.code.head2head.gwtmodules.client.services.FacadeServices;
import com.google.code.head2head.gwtmodules.client.services.FacadeServicesAsync;
import com.google.code.head2head.gwtmodules.client.util.ServiceLocator;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.MessageBox;
import com.gwtext.client.widgets.event.ButtonListener;
import com.gwtext.client.widgets.form.Form;
import com.gwtext.client.widgets.form.FormConfig;
import com.gwtext.client.widgets.form.TextField;
import com.gwtext.client.widgets.form.TextFieldConfig;

public class H2HLoginPanel extends VerticalPanel {

	private String userLoginName;

	private Long userId;

	private HTML loginText;
	
	private Form form;

	private  TextField passwdField = null;

	private  TextField userField = null;

	public H2HLoginPanel() {
		initPanel();
	}

	private void initPanel() {
		
		userLoginName = null;
		userId = null;
		loginText = new HTML("No user logged");

		 form = new Form(new FormConfig() {
			{
				setWidth(300);
				setHeader("Login Form");
				setLabelWidth(75);
				setSurroundWithBox(true);
			}
		});

		userField = new TextField(new TextFieldConfig() {
			{
				setFieldLabel("User Name");
				setName("username");
				setWidth(175);
				setAllowBlank(false);
				
			}
		});
		form.add(userField);
		

		passwdField = new TextField(new TextFieldConfig() {
			{
				setPassword(true);
				setFieldLabel("Password");
				setName("passwd");
				setWidth(175);
				setAllowBlank(false);
			}
		});
		form.add(passwdField);

		Button login=new Button("Login");
		login.addButtonListener(new ButtonListener(){

			public void onClick(Button button, EventObject e) {
				login(userField.getValueAsString(), passwdField
						.getValueAsString());
			}

			public void onMouseOut(Button button, EventObject e) {
				// TODO Auto-generated method stub
				
			}

			public void onMouseOver(Button button, EventObject e) {
				// TODO Auto-generated method stub
				
			}

			public void onToggle(Button button, boolean pressed) {
				// TODO Auto-generated method stub
				
			}
			
		});
		form.addButton(login);   
		form.render(); 
		
		this.add(loginText);
		this.add(form);

	}

	private void login(final String userLogin, final String userPassword) {

		FacadeServicesAsync svc = ServiceLocator.getFacadeServicesAsync();

		AsyncCallback callback = new AsyncCallback() {
			public void onSuccess(Object result) {
				userId = ((Long) result);
				
				if (userId!=null) {
					userLoginName = userLogin;
					clear();
					loginText.setHTML("User logged in: " + userLoginName);
					add(loginText);
					addLogoutButton();
					MessageBox.alert("Message", "Ok user " + userLoginName
							+ " logged in");

				} else {
					MessageBox.alert("Message",
							"Invalid login, please try again");
				}
			}

			public void onFailure(Throwable ex) {
				MessageBox.alert("Message", "Problem logging user");
			}
		};
		// execute the service
		svc.login(userLogin, userPassword, callback);
	}

	private void addLogoutButton() {
		Button button = new Button("logout");
		button.addButtonListener((new ButtonListener() {

			public void onClick(Button button, EventObject e) {
				logout();
				clear();
				initPanel();
			}

			public void onMouseOut(Button button, EventObject e) {
				// TODO Auto-generated method stub
				
			}

			public void onMouseOver(Button button, EventObject e) {
				// TODO Auto-generated method stub
				
			}

			public void onToggle(Button button, boolean pressed) {
				// TODO Auto-generated method stub
				
			}

		}));
		add(button);

	}

	private void logout() {
		FacadeServicesAsync svc = (FacadeServicesAsync) GWT
				.create(FacadeServices.class);
		ServiceDefTarget endpoint = (ServiceDefTarget) svc;
		String moduleRelativeURL = GWT.getModuleBaseURL()
				+ "../userInfoService";
		endpoint.setServiceEntryPoint(moduleRelativeURL);

		AsyncCallback callback = new AsyncCallback() {

			public void onFailure(Throwable arg0) {
				MessageBox.alert("Message", "Problem logging out user");

			}

			public void onSuccess(Object arg0) {
				// TODO Auto-generated method stub
				MessageBox.alert("Message", "User logged out");
			}

		};
		svc.logout(callback);
	}
	

	
}
